package com.syq.service.impl;


import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import com.syq.dao.CoachDao;
import com.syq.dao.RedisDao;
import com.syq.dto.DTO;
import com.syq.dto.MessageEnum;
import com.syq.entity.Account;
import com.syq.entity.Coach;
import com.syq.page.PageInfo;
import com.syq.service.AccountService;
import com.syq.service.MailSendService;
import com.syq.service.CoachService;
import com.syq.utils.GenNumTools;
import com.syq.utils.MailMessageUtils;
import com.syq.vo.CoachVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Service("coachService")
public class CoachServiceImpl implements CoachService, AccountService {
    @Resource
    CoachDao coachDao;
    @Resource
    MailSendService mailSendService;
    @Resource
    RedisDao redisDao;
    @Override
    public DTO<Coach> getAllCoachs()  throws Exception{
        List<Coach> coachs = coachDao.getAllCoachs();
        MessageEnum messageEnum  = Optional.ofNullable(coachs).isPresent()?MessageEnum.GETPARENTSSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coachs);
    }
    @Override
    public DTO<Coach> selectCoachByCno(String cno)  throws Exception{
        Coach coachs = coachDao.selectCoachByCno(cno);
        MessageEnum messageEnum  = Optional.ofNullable(coachs).isPresent()?MessageEnum.GETPARENTSSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coachs);
    }
    @Override
    public DTO<Coach> deleteCoachByCno(String cno)  throws Exception{
        Integer result = coachDao.deleteCoachByCno(cno);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEPARENTSSUCCESS:MessageEnum.DELETEPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Coach> updateCoachByCno(Coach coach)  throws Exception{
        Integer result = coachDao.updateCoachByCno(coach);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEPARENTSSUCCESS:MessageEnum.UPDATEPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Coach> addCoach(Coach coach) throws Exception {
         //coach.setPstate("未激活");
        Integer result = coachDao.addCoach(coach);
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDPARENTSSUCCESS:MessageEnum.ADDPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coach);
    }

    @Override
    public DTO<Coach> getCoachByPage(PageInfo pageInfo ) throws Exception {
        List<Coach> coachs = coachDao.getCoachByPage(pageInfo);
        MessageEnum messageEnum  = Optional.ofNullable(coachs).isPresent()?MessageEnum.ADDADMINSUCCESS: MessageEnum.GETPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coachs,pageInfo);
    }

    @Override
    public DTO<Coach> selectCoachWithCourseTemplate(String cno)  throws Exception{
        Coach coach = coachDao.selectCoachWithCourseTemplate(cno);
        MessageEnum messageEnum  = Optional.ofNullable(cno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coach);
    }
//
//    @Override
//    public DTO<Coach> getCoachWithOrdrees(String cno) throws Exception {
//        Coach coach = coachDao.getCoachWithOrdrees(cno);
//        MessageEnum messageEnum  = Optional.ofNullable(cno).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coach);
//    }
//
//    @Override
//    public DTO<Coach> sendCodeMail(String cname) throws Exception {
//        List<Coach> coachs = coachDao.getCoachByExample(cname);
//        DTO<Coach> dto = null;
//        MessageEnum messageEnum  = null;
//       if(!Optional.ofNullable(coachs).isPresent()){
//           messageEnum = Optional.ofNullable(coachs).isPresent()?MessageEnum.GETPARENTSBYEXAMPLESUCCESS:MessageEnum.GETPARENTSBYEXAMPLEFAIL;
//       }else{
//           String code = GenNumTools.addZero(4,(Math.abs(new Random().nextInt()%1000)));
//           redisDao.set(coachs.get(0).getCno(),code);
//           String[] mail = MailMessageUtils.getMessage(coachs.get(0),"Coach:sendCode;"+code);
//           mailSendService.sendMail(coachs.get(0),mail[0],mail[1]);
//           messageEnum = MessageEnum.ADDACTIVITYPESUCCESS;
//       }
//        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(), coachs);
//    }
//
//    @Override
//    public DTO<Coach> updateCoachByPwd(CoachVo coachVo) throws Exception {
//        String code = redisDao.get(coachVo.getCno());
//        if(!coachVo.getCode().equals(code)){
//            return new DTO<>(401,"验证码不正确");
//        }
//        Coach coach = new Coach();
//        coach.setCno(coachVo.getCno());
//        coach.setCname(coachVo.getUsername());
//        String algorithmName = "md5";
////        String coachname = coachVo.getPrentsname();
//        String coachname = coachVo.getUsername();
//        String password = coachVo.getPassword();
//        String salt1 = coachname;
//        String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
//        int hashIterations = 3;
//        SimpleHash hash = new SimpleHash(algorithmName, password, salt1 + salt2, hashIterations);//参数1选择md5加密方式，参数2 原始密码  参数3 一个特定值（salt1 用户名 ）
//        // +salt 2随机值 作为加工的佐料，参数4 佐料加的次数
//        String encodedPassword = hash.toHex();//hash出来的是128位的2进制方便存储转换为32为的字符串
//        System.out.println("000000000000000000000000");
//        System.out.println(encodedPassword);
//        coach.setPpwd(encodedPassword);
//        coachDao.updateCoachByCno(coach);
//        MessageEnum messageEnum  = Optional.ofNullable(coachVo).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<Coach>(messageEnum.getCode(),messageEnum.getMessage(),coach);
//    }
//
    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
//        Coach coach = coachDao.getAccountByAccountName(accountName);
//        System.out.println(coach+"--------666666---------");
//        MessageEnum messageEnum  = Optional.ofNullable(accountName).isPresent()?MessageEnum.GETPARENTSSUCCESS:MessageEnum.GETPARENTSFAIL;
//        return new DTO<Account>(messageEnum.getCode(),messageEnum.getMessage(),coach);
        return null;
    }
//
//
//
//    @Override
//    public DTO<Coach> activate(String cno) throws Exception {
//        Coach coach = new Coach();
//        coach.setCno(cno);
//        coach.setPstate("已激活");
//        Integer result = coachDao.updateByPrimaryKeySelective(coach);
//        //isPresent表示判断不为空成立则dto为？与：之间的对象
//        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
//        return new DTO<Coach>(messageEnum.getCode(), messageEnum.getMessage());
//    }



}
